/*
 * @Author: szx
 * @Date: 2022-02-11 14:54:22
 * @LastEditTime: 2022-02-11 15:02:42
 * @Description:
 * @FilePath: \leetcode\1900-1999\1984\1984.js
 */
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var minimumDifference = function (nums, k) {
    nums.sort((a, b) => a - b);
    let res = Number.MAX_SAFE_INTEGER;
    for (let i = 0; i < nums.length - k + 1; i++) {
        res = Math.min(nums[k + i - 1] - nums[i], res);
    }
    return res;
};

console.log(minimumDifference([9, 4, 1, 7], 2));
